/**
 * Created by thanatos on 15-9-13.
 */

var path = require('path')
var fs = require('fs')
var utils = require('utility')

module.exports = {

    trimspace: function (str) {
        return str.replace(/(^\s+)|(\s+$)/g, '').replace(/\s+/g, ' ')
    },

    //将url的参数封装成对象
    params2obj: function(query){
        var params = {}
        for (var key in query) {
            if (key.indexOf('.') == -1) {
                params[key] = query[key]
            } else {
                key.split('.').reduce(function (pre, cur, index, array) {
                    if (index == 1) {
                        if (!params[pre]) {
                            params[pre] = {}
                        }
                        pre = params[pre]
                    }
                    if (index == array.length - 1) {
                        pre[cur] = pre[cur] ? [].concat(pre[cur], query[key]) : query[key]
                        //pre[cur] = query[key]            //我们使用覆盖，也就是抛弃多选
                    } else {
                        if (!pre[cur])
                            pre[cur] = {}
                        return pre[cur]
                    }
                })
            }
        }
        return params
    },

    // 将参数封装成a=1&b=1形式
    merge4params: function () {
        var params = []
        for(var i=0; i<arguments.length; i++){
            var arg = arguments[i]
            if(arg){
                switch(typeof(arg)){
                    case 'string':
                        params.push(arg)
                        break
                    case 'object':
                        for(var key in arg){
                            if(arg[key] && arg[key]!='undefined'){
                                params.push(key+'='+arg[key])
                            }
                        }
                        break
                }
            }
        }
        return params.join('&')
    },

    // 保留指定的属性
    remain: function () {
        if(arguments.length>1){
            var obj = arguments[0]
            var result = {}
            for(var i=1; i<arguments.length; i++){
                var key = arguments[i]
                result[key] = obj[key] || null
            }
            return result
        }
    },

    // 级联创建目录
    mkdirs: function (dirname, callback) {
        var callee = arguments.callee
        fs.exists(dirname, function (exists) {
            if(!exists){
                var parent_dirname = path.dirname(dirname)
                callee(parent_dirname, function () {
                    fs.mkdir(dirname, function (err) {
                        if(err){
                            console.log('create dirs failure ---> ', err)
                        }
                        if(callback){
                            callback()
                        }
                    })
                })
            }else{
                if(callback){
                    callback()
                }
            }
        })
    },

    mkinfo: function () {
        var dates = utils.YYYYMMDD(',').split(',')
        var dirname = path.join(path.resolve('static'), 'uploads', 'images', dates[0], dates[1], dates[2])
        var filename = parseInt(Math.random()*1000000000000) + '.jpg'
        var filepath = path.join(dirname, filename)
        var _path = ['/uploads', 'images', dates[0], dates[1], dates[2], filename].join('/')
        return {
            dirname: dirname,
            filename: filename,
            filepath: filepath,
            path: _path
        }
    }

}